// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.search

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.departures.DeparturesActivity
import xyz.apiote.bimba.czwek.repo.FeedInfo
import xyz.apiote.bimba.czwek.repo.Line
import xyz.apiote.bimba.czwek.repo.Queryable
import xyz.apiote.bimba.czwek.repo.Stop
import xyz.apiote.bimba.czwek.repo.StopStub
import xyz.apiote.bimba.czwek.settings.feeds.FeedsSettings

class BimbaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
	val root: View = itemView.findViewById(R.id.suggestion)
	val icon: ImageView = itemView.findViewById(R.id.suggestion_image)
	val title: TextView = itemView.findViewById(R.id.suggestion_title)
	val description: TextView = itemView.findViewById(R.id.suggestion_description)
	val feedName: TextView = itemView.findViewById(R.id.feed_name)

	companion object {
		fun bind(
			queryable: Queryable,
			holder: BimbaViewHolder?,
			context: Context?,
			feeds: Map<String, FeedInfo>?,
			feedsSettings: FeedsSettings?,
			onClickListener: (Queryable) -> Unit
		) {
			when (queryable) {
				is Stop -> bindStop(queryable, holder, context, feeds, feedsSettings)
				is Line -> bindLine(queryable, holder, context, feeds, feedsSettings)
			}
			holder?.root?.setOnClickListener {
				onClickListener(queryable)
			}
		}

		fun bind(
			stopStub: StopStub,
			holder: BimbaViewHolder?,
			context: Context?,
			onClickListener: (StopStub) -> Unit
		) {
			holder?.title?.text = stopStub.name
			holder?.icon?.apply {
				setImageDrawable(stopStub.icon(context!!))
				contentDescription = context.getString(R.string.stop_content_description)
			}
			holder?.description?.text = when {
				stopStub.zone != "" && stopStub.onDemand -> context?.getString(
					R.string.stop_stub_on_demand_in_zone,
					stopStub.zone
				)

				stopStub.zone == "" && stopStub.onDemand -> context?.getString(R.string.stop_stub_on_demand)
				stopStub.zone != "" && !stopStub.onDemand -> context?.getString(
					R.string.stop_stub_in_zone,
					stopStub.zone
				)

				else -> ""
			}
			holder?.root?.setOnClickListener {
				onClickListener(stopStub)
			}
		}

		private fun bindStop(
			stop: Stop,
			holder: BimbaViewHolder?,
			context: Context?,
			feeds: Map<String, FeedInfo>?,
			feedsSettings: FeedsSettings?
		) {
			holder?.icon?.apply {
				setImageDrawable(stop.icon(context!!))
				contentDescription = context.getString(R.string.stop_content_description)
			}
			holder?.title?.text = stop.name
			if ((feedsSettings?.activeFeeds() ?: 0) > 1) {
				holder?.feedName?.visibility = View.VISIBLE
				holder?.feedName?.text = feeds?.get(stop.feedID)?.name ?: ""
			}
			context?.let {
				stop.changeOptions(it).let { changeOptions ->
					holder?.description?.apply {
						text = changeOptions.first
						contentDescription = changeOptions.second
					}
				}
			}
		}

		private fun bindLine(
			line: Line,
			holder: BimbaViewHolder?,
			context: Context?,
			feeds: Map<String, FeedInfo>?,
			feedsSettings: FeedsSettings?
		) {
			holder?.icon?.apply {
				setImageDrawable(line.icon(context!!))
				contentDescription = line.type.name
				colorFilter = null
			}
			if ((feedsSettings?.activeFeeds() ?: 0) > 1) {
				holder?.feedName?.visibility = View.VISIBLE
				holder?.feedName?.text = feeds?.get(line.feedID)?.name ?: ""
			}
			holder?.title?.text = line.name
			holder?.description?.text = if (line.headsigns.size == 1) {
				context?.getString(
					R.string.line_headsign,
					line.headsigns[0].joinToString { it })
			} else {
				context?.getString(
					R.string.line_headsigns,
					line.headsigns[0].joinToString { it },
					line.headsigns[1].joinToString { it })
			}
			holder?.description?.contentDescription = if (line.headsigns.size == 1) {
				context?.getString(
					R.string.line_headsign_content_description,
					line.headsigns[0].joinToString { it })
			} else {
				context?.getString(
					R.string.line_headsigns_content_description,
					line.headsigns[0].joinToString { it },
					line.headsigns[1].joinToString { it })
			}
		}
	}
}


class BimbaResultsAdapter(
	private val inflater: LayoutInflater,
	private val context: Context?,
	private var queryables: List<Queryable>,
) :
	RecyclerView.Adapter<BimbaViewHolder>() {
	class DiffUtilCallback(
		private val oldQueryables: List<Queryable>,
		private val newQueryables: List<Queryable>
	) : DiffUtil.Callback() {
		override fun getOldListSize() = oldQueryables.size

		override fun getNewListSize() = newQueryables.size

		override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
			val oldQueryable = oldQueryables[oldItemPosition]
			val newQueryable = newQueryables[newItemPosition]
			return if (oldQueryable::class.java != newQueryable::class.java) {
				false
			} else {
				when (oldQueryable) {
					is Line -> {
						assert(newQueryable is Line)
						oldQueryable.name == (newQueryable as Line).name // TODO compare line.ID when struct is updated
					}

					is Stop -> {
						assert(newQueryable is Stop)
						oldQueryable.code == (newQueryable as Stop).code
					}

					else -> false // XXX unreachable
				}
			}
		}

		override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
			val oldQueryable = oldQueryables[oldItemPosition]
			val newQueryable = newQueryables[newItemPosition]
			return when (oldQueryable) {
				is Line -> {
					assert(newQueryable is Line)
					val oldHeadsigns =
						oldQueryable.headsigns.joinToString { hsList -> hsList.joinToString { it } }
					val newHeadsigns =
						(newQueryable as Line).headsigns.joinToString { hsList -> hsList.joinToString { it } }

					oldQueryable.name == newQueryable.name && oldQueryable.type == newQueryable.type &&
						oldQueryable.colour == newQueryable.colour && oldHeadsigns == newHeadsigns
				}

				is Stop -> {
					assert(newQueryable is Stop)
					val oldChangeOptions =
						oldQueryable.changeOptions.joinToString { "${it.line}->${it.headsign}" }
					val newChangeOptions =
						(newQueryable as Stop).changeOptions.joinToString { "${it.line}->${it.headsign}" }
					oldQueryable.name == newQueryable.name && oldChangeOptions == newChangeOptions
				}

				else -> false // XXX unreachable
			}
		}

	}

	var feeds: Map<String, FeedInfo>? = null
	var feedsSettings: FeedsSettings? = null
	private val onClickListener: ((Queryable) -> Unit) = {
		when (it) {
			is Stop -> {
				val intent = Intent(context, DeparturesActivity::class.java).apply {
					putExtra("code", it.code)
					putExtra("name", it.name)
					putExtra("feedID", it.feedID)
				}
				context!!.startActivity(intent)
			}

			is Line -> {
				val intent = Intent(context, LineGraphActivity::class.java).apply {
					putExtra("line", it.name)
					putExtra("feedID", it.feedID)
				}
				context!!.startActivity(intent)
			}
		}
	}

	override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaViewHolder {
		val rowView = inflater.inflate(R.layout.result, parent, false)
		return BimbaViewHolder(rowView)
	}

	override fun onBindViewHolder(holder: BimbaViewHolder, position: Int) {
		BimbaViewHolder.bind(
			queryables[position],
			holder,
			context,
			feeds,
			feedsSettings,
			onClickListener
		)
	}

	override fun getItemCount(): Int = queryables.size

	fun update(queryables: List<Queryable>?) {
		val newQueryables = queryables ?: emptyList()
		val diff = DiffUtil.calculateDiff(DiffUtilCallback(this.queryables, newQueryables))
		this.queryables = newQueryables
		diff.dispatchUpdatesTo(this)
	}

	fun click(position: Int) {
		onClickListener(queryables[position])
	}
}